package com.zed.api.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

/**
 * 扩展 {@link ApplicationEventPublisherAware}接口,让具有后续绑定能力
 *
 * @author Zed
 */
public interface ApplicationEventPublisherAwareExtend extends ApplicationEventPublisherAware {

    /**
     * 尝试向{@code target}绑定设置模块
     *
     * @param target                    {@link T},目标对象
     * @param applicationEventPublisher {@link ApplicationEventPublisher},应用事件发布
     * @return {@link boolean},返回{@code true}表示调用{@link ApplicationEventPublisherAware(ApplicationContext)})}方法，否则返回{@code false}
     */
    static <T> boolean tryToBind(T target, ApplicationEventPublisher applicationEventPublisher) {
        if (target == null) return false;

        if (target instanceof ApplicationEventPublisherAware) {
            ((ApplicationEventPublisherAware) target).setApplicationEventPublisher(applicationEventPublisher);
            return true;
        }

        return false;
    }

}
